package com.cy.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cy.common.Constant;
import com.cy.entity.Message;
import com.cy.mapper.MessageMapper;
import com.cy.service.MessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cy.util.SensitiveFilter;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import javax.annotation.Resource;
import javax.swing.text.ComponentView;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cy
 * @since 2021-11-08
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {

    @Resource
    private SensitiveFilter sensitiveFilter;

    /**
     * 查询当前用户的会话列表,针对每个会话只返回一个最新的私信
     * @param userId
     * @param page
     * @return
     */
    @Override
    public IPage<Message> selectConversations(Integer userId, Page<Message> page) {
        return this.baseMapper.selectConversations(userId, page);
    }

    /**
     * 查询当前用户的会话数量
     * @param userId
     * @return
     */
    @Override
    public Integer selectConversationCount(Integer userId) {
        return this.baseMapper.selectConversationCount(userId);
    }


    /**
     * 查询某个会话所包含的私信列表
     * @param conversationId
     * @param page
     * @return
     */
    @Override
    public IPage<Message> selectLetters(String conversationId, Page<Message> page) {
        return this.baseMapper.selectLetters(conversationId, page);
    }


    /**
     * 查询某个会话所包含的私信数量
     * @param conversationId
     * @return
     */
    @Override
    public Integer selectLettersCount(String conversationId) {
        return this.baseMapper.selectLettersCount(conversationId);
    }

    /**
     * 查询未读私信的数量，
     * 如果不传conversationId则表示用户总的未读私信数量，
     * 如果传conversationId则表示某个用户某个会话的具体私信数量
     * @param userId
     * @param conversationId
     * @return
     */
    @Override
    public Integer selectLettersCountUnreadCount(Integer userId, String conversationId) {
        return  this.baseMapper.selectLettersCountUnreadCount(userId, conversationId);
    }

    @Override
    public Integer addMessage(Message message) {
        message.setContent(HtmlUtils.htmlEscape(message.getContent())); //过滤标签
        message.setContent(sensitiveFilter.filter(message.getContent()));   //敏感词过滤
        return this.baseMapper.insert(message);
    }

    @Override
    public Integer readMessage(List<Integer> ids) {
        return this.baseMapper.updateStatus(ids, 1);
    }

    // 查询某个主题下最新的通知
    @Override
    public Message selectLatestNotice(int userId, String topic) {
        Message message = this.baseMapper.selectLatestNotice(userId, topic);
        return message;
    }

    // 查询某个主题所包含的通知数量
    @Override
    public int selectNoticeCount(int userId, String topic) {
        QueryWrapper<Message> wrapper = new QueryWrapper<>();
        wrapper.ne("status", 2)
                .eq("from_id", 1)
                .eq("to_id", userId)
                .eq("conversation_id", topic);
        return this.baseMapper.selectCount(wrapper);
    }

    // 查询未读的通知的数量   如果topic为nullz则代表查询所有的未读通知数量
    @Override
    public int selectNoticeUnreadCount(int userId, String topic) {
        QueryWrapper<Message> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 0)
                .eq("from_id", 1)   //1系统消息
                .eq("to_id", userId)
                .eq(StrUtil.isNotBlank(topic),"conversation_id", topic);
        return this.baseMapper.selectCount(wrapper);
    }

    // 查询某个主题所包含的通知列表
    @Override
    public List<Message> selectNotices(int userId, String topic, int offset, int limit) {

        return  this.baseMapper.selectNotices(userId, topic, offset, limit);
    }


}
